package com.cn.controller.customerManager;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.entity.TbCompany;
import com.cn.entity.TbCustomer;
import com.cn.service.company.CompanyService;
import com.cn.service.customer.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Author 顾客信息管理
 * @Date 2021/6/4 11:32
 * @Package com.cn.controller.customerManager
 * @Version 1.0
 */

@Controller
@RequestMapping("/customerManager")
public class CustomerManagerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CompanyService companyService;

    /*
    分页查询所有的顾客
     */
    @RequestMapping("/customerList")
    public String allCustomer(String name, String idCard, Long companyId, @RequestParam(defaultValue = "1") Integer pageNum, Model model){
        Page<TbCustomer> page = customerService.allTbCustomer(name, idCard,companyId,pageNum);
        List<TbCompany> companys = companyService.companys();
        model.addAttribute("customers",page);
        model.addAttribute("name",name);
        model.addAttribute("idCard",idCard);
        model.addAttribute("companyId",companyId);
        model.addAttribute("companys",companys);
        return "customerList/saved_resource";
    }
    /*
    进入添加顾客页面
     */
    @RequestMapping("/addCustomerHtml")
    public String addCustomerHtml(){
        return "/addCustomer/saved_resource";
    }
    /*
    添加顾客
     */
    @RequestMapping("/addCustomer")
    public String addCompany(TbCustomer tbCustomer){
        customerService.addTbCustomer(tbCustomer);
        return "redirect:/customerManager/customerList";
    }
    /*
    进入修改顾客信息页面
     */
    @RequestMapping("/updateById")
    public String updateById(Long id,Model model){
        TbCustomer tbCustomer = customerService.queryById(id);
        model.addAttribute("customer",tbCustomer);
        return "/updateCustomer/saved_resource";
    }
    /*
    修改顾客信息
     */
    @RequestMapping("/updateCustomer")
    public String updateCustomer(TbCustomer tbCustomer){
        customerService.updateTbCustomer(tbCustomer);
        return "redirect:/customerManager/customerList";
    }
    /*
    根据id删除顾客信息
     */
    @RequestMapping("/delById")
    public String delById(Long id){
        customerService.delById(id);
        return "redirect:/customerManager/customerList\"";
    }
}
